<?php
namespace Admin\Controller;
use Admin\Common\Page;
class ChildSchoolController extends BaseController
{
    /**
     * 幼儿园列表
     */
    public function index()
    {
        $child = M('Child_school');
        $count = $child->where('status=0')->count();
//        $count = $m->where($map)->count();// 查询满足要求的总记录数
        $Page = new  Page($count , C('page.size'));// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show();// 分页显示输出
        $this->assign('page' , $show);// 赋值分页输出
        $childschool = $child->where('status=0')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('childschool',$childschool);
        $this->display();
    }

    /**
     * 显示添加/修改幼儿园页面
    */
    public function edit(){
        $id = I('get.id');
        if(empty($id)){
            $this->display();
        }else{
            $data = M('Child_school')->where("id=$id")->find();
//            $bagku =M('bagku')->select();
            $this->assign('data',$data);
//            $this->assign('bagku',$bagku);
            $this->display();
        }

    }

    /**
     * do  添加|修改幼儿园
     */
    public function doedit(){
        $id = I('get.id');
        $data['name']=I("post.name");
        $data['bagku_id']=I("post.bagku_id");
        $chilschool = M('Child_school');
        if($id == ""){
            $id=0;
        }
        $count=$chilschool->where("id=$id")->count();
        if($count>0){
            $chilschool->where("id=$id")->data($data)->save();
            $this->success("修改成功",U("index"));
        }else{
            $chilschool->data($data)->add();
            $this->success("添加成功",U("index"));
        }
    }

    /**
     * 查看幼儿园下的班级
     */
    public function classlist(){
        $id = I('get.id');
        $childname= M('Child_school')->where("id = $id")->getField('name');
        $classdata =M('Class')->where("child_school_id=$id")->select();
        $this->assign('classdata',$classdata);
        $this->assign('childname',$childname);
        $this->display();

    }
    /**
     * 查看班级下的学生
     */
    public function classstu(){
        $class_id = I('get.class_id');
        $classname =M('Class')->where("class_id=$class_id")->getField('class_name');
        $studata   = M('Student')->where("class_id = $class_id ")->select();
        $this->assign('classdata',$classname);
        $this->assign('studata',$studata);
        $this->display();

    }

    /**
     * 删除幼儿园
     */
    public function del(){
        $id=I('get.id');
        if($id == ""){
            $this->error('参数错误');
        }
        /*首先查询该幼儿园有多少班级*/
        $classIds = M('class')->where('status=0 and child_school_id='.$id)->getField('id',true);
        if($classIds==NUll){
            M('child_school')->where('id='.$id)->save(array('status'=>4));
            $this->success('删除成功',U("index"));
        }
        /*如果有班级查询班级下是否还有未解除绑定的学生*/
        $classStr = trim(implode(',',$classIds),',');

        $data = M()->table('class as c,student as s,card')->where('s.card_id=card.card_id and s.class_id=c.id and s.status=1 and s.class_id in ('.$classStr.')')->field('card.child_name,c.class_name')->select();
        //dump($data);die;
        $errMsg = '';
        if($data){
            foreach ($data as $k=>$v){
                $errMsg .=$v['class_name']."的".$v['child_name']."、";
            }
            $this->error('该幼儿园还有学生绑定着书包:'.$errMsg);
        }
        /*将班级下所有学生删除*/
        $db = M();
        $db->startTrans();
        $key1 = $db->table('class as c,student as s,card')->where('s.card_id=card.card_id and s.class_id=c.id and s.status=0 and s.class_id in ('.$classStr.')')->save(array('s.status'=>4));
        $key2 = $db->table('child_school')->where('id='.$id)->save(array('status'=>4));
        $key3 = $db->table('class')->where('status=0 and child_school_id='.$id)->save(array('status'=>4));
        if($key1 && $key2 && $key3){
            $db->commit();
            $this->success('删除成功',U('index'));
        }else{
            $db->rollback();
            $this->error('删除成功');
        }


    }



    /**
     * 导出幼儿园列表
    */
    public function export(){
        $Card = M('Child_school');

        $d = $Card->field('id,name')->select();
        $file = excel_export($d);
        ouput_Browser($file);
    }
}